<?php

namespace app\dev\controller;

use think\Controller;
use app\common\model\book\Info as BookModel;
use app\common\model\book\Bag as BagModel;
use app\common\model\book\Same;
use app\common\model\job\Books as JobBookModel;
use app\common\model\book\topic\Books as TopicBookModel;
use app\common\model\book\Board as BoardBookModel;



class Book extends Controller{
    /**
     * 书籍数量校对
     */
    public function checkBook()
    {
        $bookModel = new BookModel();
        $books = $bookModel->where('total_num > 0')->whereOr('ava_num > 0')->whereOr('out_num > 0')->whereOr('borrowed_time > 0')->whereOr('lost_num > 0')->fetchSql(0)->field('id,name,isbn,total_num,ava_num,out_num,lost_num')->select();
        $error = [];
        $html = '<style>tr,td{border:1px solid #333;}</style>';
        $html .= '<table>';
        $html .= '<tr>';
        $html .= '<td>序號</td>';
        $html .= '<td>id</td>';
        $html .= '<td>isbn</td>';
        $html .= '<td>书名</td>';
        $html .= '<td>总数</td>';
        $html .= '<td>库存</td>';
        $html .= '<td>外借数量</td>';
        $html .= '<td>计划外借数量</td>';
        $html .= '<td>实际外借数量</td>';
        $html .= '<td>遗失数量</td>';
        $html .= '</tr>';

        $bagModel = new BagModel();
        foreach ($books as $key => $value) {
            // $error[] = $value;
            $out = $bagModel->where('book_ids', $value->id)->where('status','in','0,1,2')->count();
            $plane = $bagModel->where('book_ids', $value->id)->where('status','-1')->count();

            $style = '';

            if($value->total_num != $value->ava_num + $value->out_num + $value->lost_num + $plane){
                $style = 'style="background:yellow;"';
            }

            $html .= '<tr '.$style.'>';
            $html .= '<td>'.($key + 1).'</td>';
            $html .= '<td>'.$value->id.'</td>';
            $html .= '<td>'.$value->isbn.'</td>';
            $html .= '<td>'.$value->name.'</td>';
            $html .= '<td>'.$value->total_num.'</td>';
            $html .= '<td>'.$value->ava_num.'</td>';
            $html .= '<td>'.$value->out_num.'</td>';
            $html .= '<td>'.$plane.'</td>';
            $html .= '<td>'.$out.'</td>';
            $html .= '<td>'.$value->lost_num.'</td>';
            $html .= '</tr>';
        }
        $html .= '</table>';
        return $html;
    }

    public function rebuildes(){
        $res = \app\common\service\BookEs::rebuild();

        var_dump($res);
    }


    public function chkbag(){
        $bag = new \app\common\model\book\Bag();

        $html = '<style>tr,td{border:1px solid #333;}</style>';
        $html .= '<table>';
        $html .= '<tr>';
        $html .= '<td>序號</td>';
        $html .= '<td>用户昵称</td>';
        $html .= '<td>用户电话</td>';
        $html .= '<td>书名</td>';
        $html .= '<td>计划送达日期</td>';
        $html .= '<td>书盒发出时间</td>';
        $html .= '<td>存在疑问</td>';
        $html .= '<td>备注</td>';
        $html .= '</tr>';

        $user_id = [];

        $bags = collect($bag->where('status','1')->whereOr("id",381)->select())->each(function($item,$key) use(&$html, &$user_id){
            $user = $item->user_info;
            $book = $item->books;
            array_push($user_id, $user->id);
            $html .= '<tr>';
            $html .= '<td>'.$item->id.'</td>';
            $html .= '<td>'.$user->nickname.'</td>';
            $html .= '<td>'.$user->mobile.'</td>';
            $html .= '<td>'.$book->name.'</td>';
            $html .= '<td>'.$item->plane_date.'</td>';
            $html .= '<td>'.date('Y-m-d H:i:s', $item->sendtime).'</td>';
            $html .= '<td>'. ($item->status == '1' ? '不知道书盒是否送达':'不知道是否收到书盒') .'</td>';
            $html .= '<td></td>';
            $html .= '</tr>';
        });

       
 
        $html .= '<table>';
        $html .= '<tr>';
        $html .= '<td>序號</td>';
        $html .= '<td>用户昵称</td>';
        $html .= '<td>企业</td>';
        $html .= '<td>用户电话</td>';
        $html .= '<td width="220px">备注</td>';
        $html .= '</tr>';

        $user = collect(\app\common\model\User::where("id",'not in', $user_id)->where('com_info_id','>',0)->order('com_info_id asc')->select())->each(function($user,$key) use(&$html){
            $com = $user->com_info;
            $html .= '<tr>';
            $html .= '<td>'.$user->id.'</td>';
            $html .= '<td>'.$com->short_name.'</td>';
            $html .= '<td>'.$user->nickname.'</td>';
            $html .= '<td>'.$user->mobile.'</td>';
            $html .= '<td></td>';
            $html .= '</tr>';
        });


        return $html;
    }

    public function deletebooks(){
        $isbns = getIsbnFromStr('9787806778876 9787801556165 9787303064946 9787801474667 ');

        foreach ($isbns as $key => $isbn) {
            if(strlen($isbn)==10){
                $isbns[] = '978'.$isbn;
            }
        }



        // var_dump($isbns);die;

        $bookids = BookModel::where('isbn','in',$isbns)->column('id');

        $bagBook_ids = [];

        foreach ($bookids as $key => $bookid) {
            if(BagModel::where('book_ids',$bookid)->find()){
                echo "书籍：{$bookid}，被书盒使用无法删除\n";
                $bagBook_ids[] = $bookid;
            }else{
                try {
                    \app\common\service\BookEs::updateById($bookid);
                } catch (\Exception $e) {
                    echo "{$e->getMessage()}\n";
                }

                #直接库里删除
                BookModel::where('id',$bookid)->delete();
            }
        }

        if($bagBook_ids)
        $bookids = array_diff($bookids, $bagBook_ids);

        #从相同书籍中删除
        $same = Same::where('isbn','in',$isbns)->delete();

        echo "从相同书籍中删除： {$same}本\n";

        #把落单的删除
        #在数据库上操作吧
        

        #从职业书籍中删除
        $job = JobBookModel::where('book_id','in',$bookids)->delete();

        echo "职业书籍删除： {$job}本\n";

        #从榜单中删除
        
        $job = BoardBookModel::where('book_id','in',$bookids)->delete();

        echo "从榜单中删除： {$job}本\n";

        #从书单中删除
        
        $job = TopicBookModel::where('book_id','in',$bookids)->delete();

        echo "从书单中删除： {$job}本\n";


        #从书库中隐藏
    
        $job = BookModel::where('id','in',$bookids)->update([
            'status' => 0
        ]);

        echo "从书库中隐藏： {$job}本\n";

    }
}